﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 软构大作业
{
    public partial class Form6 : Form
    {
        private new AutoAdaptWindowsSize AutoSize;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        public Form6()
        {
            InitializeComponent();
            checkBox1.CheckedChanged += checkBox1_CheckedChanged;
            // 设置窗体的KeyPreview属性为true，以便窗体可以接收键盘事件
            this.KeyPreview = true;
            // 为窗体添加KeyDown事件处理
            this.KeyDown += Form6_KeyDown;
        }
        private void Form6_FormClosed(object sender, FormClosedEventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            Global.Instance.form6login = null;
            Global.Instance.form5login.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            Global.Instance.form6login.Hide();
            Global.Instance.form5login.Show();
        }
        private void Form6_KeyDown(object sender, KeyEventArgs e)
        {
            // 检查是否按下Enter键
            if (e.KeyCode == Keys.Enter)
            {
                // 触发button1的点击事件
                button1.PerformClick();
                // 标记事件已处理，防止其他控件处理该事件
                e.Handled = true;
                e.SuppressKeyPress = true; // 防止发出"叮"的声音
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                login();
                
            }
            else
            {
                MessageBox.Show("用户名和密码不能为空");
            }
        }
        public Boolean login()
        {
            Data dao = new Data();
            string sql = $"select * from library_db.admins where username='{textBox1.Text}' and password ='{textBox2.Text}'";
            IDataReader dc = dao.read(sql);
            if (dc.Read())
            {
                //MessageBox.Show("登陆成功");
                if (Global.Instance.form7login == null)
                {
                    Form7 form7 = new Form7();
                    Global.Instance.form7login = form7;
                }
                textBox1.Text = "";
                textBox2.Text = "";
                Global.Instance.form7login.Show();
                Global.Instance.form6login.Hide();
                return true;
            }
            else
            {
                MessageBox.Show("登陆失败，用户名或密码错误");
                return false;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            textBox2.UseSystemPasswordChar = !checkBox1.Checked;
        }

        private void Form6_Load(object sender, EventArgs e)
        {
            AutoSize = new AutoAdaptWindowsSize(this);
            this.SizeChanged += new EventHandler(Form_SizeChanged);
        }

        private void Form_SizeChanged(object sender, EventArgs e)
        {//窗体大小改变事件
            if (AutoSize != null) // 一定加这个判断，电脑缩放布局不是100%的时候，会报错
            {
                AutoSize.FormSizeChanged();
            }
        }
    }
}
